import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  build: {
    outDir: 'build',
    sourcemap: false,
    rollupOptions: {
      output: {
        manualChunks: {
          // 将页面组件打包到单独的 chunk 中
          'pages': [
            './src/pages/home/index.tsx',
            './src/pages/Login/index.tsx',
            './src/pages/Device/index.tsx',
            './src/pages/shop/commodity.tsx',
            './src/pages/shop/order.tsx',
            './src/pages/agent/agent.tsx',
          ]
        }
      }
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        modifyVars: {
          // Ant Design 主题变量
          '@primary-color': '#1890ff',
          '@link-color': '#1890ff',
          '@success-color': '#52c41a',
          '@warning-color': '#faad14',
          '@error-color': '#f5222d',
          '@font-size-base': '14px',
          '@heading-color': 'rgba(0, 0, 0, 0.85)',
          '@text-color': 'rgba(0, 0, 0, 0.65)',
          '@text-color-secondary': 'rgba(0, 0, 0, 0.45)',
          '@disabled-color': 'rgba(0, 0, 0, 0.25)',
          '@border-radius-base': '4px',
          '@border-color-base': '#d9d9d9',
          '@box-shadow-base': '0 2px 8px rgba(0, 0, 0, 0.15)'
        },
      },
    },
  },
  server: {
    port: 3005,
    host: '0.0.0.0', // 允许所有IP访问，包括局域网IP
    historyApiFallback: true, // 支持React Router的history模式
    proxy: {
      // 代理所有 /api 开头的请求
      '/api': {
        target: 'https://apitest.awarelife.cn',
        changeOrigin: true,
        secure: true,
        rewrite: (path) => path.replace(/^\/api/, '') // 重写路径，去掉 /api 前缀
      },
      
      // 代理 v1 版本的API
      '/v1': {
        target: 'https://apitest.awarelife.cn',
        changeOrigin: true,
        secure: true
      }
    }
  },
}) 